Skip to content

dbfile: fix use-after-free and leak when recreating a rejected hashfile - #413

Open
martinus wants to merge 1 commit into
markfasheh:masterfrom
martinus:backport/hashfile-recreate-uaf
Open

dbfile: fix use-after-free and leak when recreating a rejected hashfile#413
martinus wants to merge 1 commit into
markfasheh:masterfrom
martinus:backport/hashfile-recreate-uaf

Conversation

@martinus

Copy link
Copy Markdown

What this fixes

When an existing hashfile fails dbfile_check() (a schema-version or hash-type mismatch — e.g. opening an older hashfile with a newer duperemove), dbfile_prepare() takes the "Recreating hashfile" path: it sqlite3_close()s the handle, unlinks the file, reopens a fresh one, and recurses.

But the handle was passed by value, so the reopened handle only ever lived in dbfile_prepare()'s local variable. Its caller, __dbfile_open_handle(), kept — and returned — a pointer to the closed handle.

What happens without the fix

__dbfile_open_handle()'s caller then runs sqlite3_prepare_v2() on that freed handle — a use-after-free (SQLITE_MISUSE / "Database error 21") — and later sqlite3_close()s it a second time, while the real working handle opened during recreation is leaked (~160 KB, "definitely lost" under valgrind). Normal runs happen to read the freed-but-intact memory and pass, so it's a latent defect; valgrind surfaces it reliably whenever a hashfile is rebuilt.

The fix

Pass the handle by reference (sqlite3 **) so the freshly-opened handle propagates back to the caller, and bail out cleanly if the reopen fails (the previous code would also have recursed into dbfile_prepare(NULL)).

Found via valgrind in the oans fork, whose strict hashfile branding makes the recreate path easy to hit — but the defect is inherited straight from here.

When an existing hashfile fails dbfile_check() (a schema version or
hash-type mismatch), dbfile_prepare() takes the "Recreating hashfile"
path: it sqlite3_close()s the handle, unlinks the file, reopens a fresh
one, and recurses. But the handle was passed by value, so the reopened
handle only ever lived in dbfile_prepare()'s local variable. Its caller,
__dbfile_open_handle(), kept - and returned - a pointer to the *closed*
handle.

__dbfile_open_handle()'s caller then ran sqlite3_prepare_v2() on that
freed handle (use-after-free; SQLITE_MISUSE / "Database error 21"), and
later sqlite3_close()d it again, while the working handle opened during
recreation was leaked (~160 KB, "definitely lost" under valgrind).

Pass the handle by reference (sqlite3 **) so the reopened handle
propagates back to the caller, and bail out cleanly if the reopen fails
(the previous code would also have recursed into dbfile_prepare(NULL)).

Normal runs happened to read the freed-but-intact memory and passed;
valgrind exposes it whenever a hashfile is rebuilt - e.g. opening an
older-schema hashfile with a newer duperemove.

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant